package com.hyl.controller;

import com.hyl.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * LoginController
 *
 * @author hyl
 * @date 2024-12-18
 */
@Controller
public class LoginController {
//    @RequestMapping("/login")
//    public ModelAndView login() {
//        ModelAndView mav = new ModelAndView("login");
//        return mav;
//    }

    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public ModelAndView tologin1() {
        ModelAndView mav = new ModelAndView("login");
        return mav;
    }

    @GetMapping(value = "/login2")
    public ModelAndView tologin2() {
        ModelAndView mav = new ModelAndView("login");
        return mav;
    }


    @PostMapping("/login3")
    public String login1(@RequestParam(
            name = "name",//页面中传递的参数名称 name =》赋值=》username
            required = false, //required默认为true，页面中这个参数必须有值
            defaultValue = "1" //默认值，如果name没有传参，则使用默认值
            ) String username,Integer password,
                        Model model){
        model.addAttribute("username", username);
        return "index";
    }

    @PostMapping("/login")
    public String login(User user){
        System.out.println("用户名："+user.getName());
        return "index";
    }


    @GetMapping("/test")
    @ResponseBody
    public User test(){
        User user = new User();
        user.setName("admin");
        return user;
    }


}
